package com.lastproject.web.bean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ArrayDataModel;
import javax.faces.model.DataModel;

import com.lastproject.dto.QuestionDTO;
import com.lastproject.dto.QuestionOptionDTO;
import com.lastproject.dto.SurveyDTO;
import com.lastproject.ejb.dao.QuestionDAORemote;
import com.lastproject.ejb.dao.QuestionOptionDAORemote;
import com.lastproject.ejb.dao.SurveyDAORemote;

@ManagedBean
@RequestScoped
public class SurveyAnswerStatsTableData {

	private DataModel<SurveyAnswerStatsBean> filterModel = null;

	@ManagedProperty(value = "#{surveyAnswerStatsBean}")
	private SurveyAnswerStatsBean survAnswerStats; // cb

	private SurveyAnswerStatsBean[] questionList; // customers

	private SurveyQOBean[] surveyQOBeanArr;

	@EJB
	private SurveyDAORemote survDAOr;

	@EJB
	private QuestionDAORemote questionDAOr;

	@EJB
	private QuestionOptionDAORemote questionOptionDAOr;

	@PostConstruct
	public void postConstr() {

		Integer idSurv = Integer.parseInt(FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("idSurv"));
		survAnswerStats = new SurveyAnswerStatsBean(idSurv);
		
		List<QuestionDTO> qList = questionDAOr
				.getQuestionListBySurveyId(survAnswerStats.getIdSurv());
		List<QuestionOptionDTO> qoList;

		QuestionDTO[] qdtoArr = new QuestionDTO[qList.size()];
		QuestionOptionDTO[] qOptionDtoArr;

		qList.toArray(qdtoArr);

		questionList = new SurveyAnswerStatsBean[qList.size()];

		for (int i = 0; i < qdtoArr.length; i++) {
			questionList[i] = new SurveyAnswerStatsBean(qdtoArr[i]);

			qoList = questionOptionDAOr
					.getQuestionOptionListByQuestionId(questionList[i]
							.getQuestionDTO().getId());

			surveyQOBeanArr = new SurveyQOBean[qoList.size()];
			questionList[i].setQuestionOptionDtoList(qoList);

			System.out.println(questionList[i].getQuestionDTO().getText());

			for (int j = 0; j < qoList.size(); j++) {
				surveyQOBeanArr[j] = new SurveyQOBean(qoList.get(j));
				System.out.println("Question Option: "
						+ qoList.get(j).getText());
			}
			questionList[i].setSurveyQOBeanArr(surveyQOBeanArr);

		}

		filterModel = new ArrayDataModel<SurveyAnswerStatsBean>(questionList);

	}

	public SurveyAnswerStatsTableData() {

	}

	public SurveyAnswerStatsBean getSurvAnswerStats() {
		return survAnswerStats;
	}

	public void setSurvAnswerStats(SurveyAnswerStatsBean survAnswerStats) {
		this.survAnswerStats = survAnswerStats;
	}

	public DataModel<SurveyAnswerStatsBean> getQuestionList() {
		return filterModel;
	}

}
